//
//  BaseViewController.m
//  OrderSeat
//
//  Created by wangwei on 13-10-9.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (id)initWithNibClass:(Class)_class
{
    return [super initWithNibName:NSStringFromClass(_class) bundle:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (!self.data) {
        self.data = [NSMutableDictionary dictionary];
    }
    
    if (!self.controls) {
        self.controls = [NSMutableDictionary dictionary];
    }
}

- (void)reset
{
    self.isLoaded = YES;
    self.isBack = NO;
}

- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle
{
    [[AppDelegate sharedInstance].application setStatusBarStyle:statusBarStyle];
}

- (void)setNavigationTitle:(NSString*)title
{
    self.navigationItem.title = title;
    
    UILabel *titleView = [[UILabel alloc] init];
    titleView.frame = CGRectMake(0, 0, 140, 44);
    titleView.font = [UIFont boldSystemFontOfSize:18];
    titleView.text = title;
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.backgroundColor = [UIColor clearColor];
    titleView.textColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = titleView;    
}

- (void)createNavigation:(NSString*)title
{
    [self setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:NAV_BACKGROUND] forBarMetrics:UIBarMetricsDefault];
    
    [self setNavigationTitle:title];
}

- (void)createLeftButton
{
    [self createLeftButtonWithTitle:nil background:nil frame:CGRectMake(0, 0, 56, 30)];
}

- (void)createRightButton
{
    [self createRightButtonWithTitle:nil background:nil frame:CGRectMake(0, 0, 56, 30)];
}

- (void)createRightButton:(NSString*)title
{
    [self createRightButtonWithTitle:title background:nil frame:CGRectMake(0, 0, 56, 30)];
}

- (void)createRightButtonWithFrame:(CGRect)rect
{
    [self createRightButtonWithTitle:@"" background:nil frame:rect];
}

- (void)createRightButtonWithFrame:(CGRect)rect background:(NSString*)background
{
    [self createRightButtonWithTitle:@"" background:background frame:rect];
}

- (void)createLeftButtonWithTitle:(NSString*)title background:(NSString*)background frame:(CGRect)rect
{
    if (!title) title = BACK_BUTTON_TITLE;
    if (!background) background = NAV_BUTTON_BACKGROUND;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 56, 30);
    [button setBackgroundImage:[UIImage imageNamed:background] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton =[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)createRightButtonWithTitle:(NSString*)title background:(NSString*)background frame:(CGRect)rect
{
    if (!title) title = BACK_BUTTON_TITLE;
    if (!background) background = NAV_BUTTON_BACKGROUND;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = rect;
    [button setBackgroundImage:[UIImage imageNamed:background] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)createRightButtonWithTitle:(NSString*)title background:(NSString*)background
{
    if (!title) title = BACK_BUTTON_TITLE;
    if (!background) background = NAV_BUTTON_BACKGROUND;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:background] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)leftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonClick:(id)sender
{
    //
}

- (id)createCellWithNib:(NSString*)nib
{
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:nib owner:self options:nil];
    UITableViewCell *cell = [array objectAtIndex:0];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (id)createCellWithNib:(NSString*)nib tableview:(UITableView*)tableview
{
    UITableView *cell = [tableview dequeueReusableCellWithIdentifier:nib];
    if (cell == nil) {
        cell = [self createCellWithNib:nib];
    }
    
    return cell;
}

- (void)pushViewController:(UIViewController *)viewController
{
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)popToViewController:(UIViewController *)viewController
{
    [self.navigationController popToViewController:viewController animated:YES];
}

- (void)pushOrPopViewController:(Class)_class
{
    id viewController = [self findControllerWithClass:_class];
    if (viewController) {
        [self popToViewController:viewController];
    }else {
        viewController = [[_class alloc] init];
        [self pushViewController:viewController];
    }
}

- (BOOL)findController:(UIViewController*)viewController
{
    for(UIViewController *viewControl in self.navigationController.viewControllers) {
        if ([NSStringFromClass(viewController.class) isEqualToString:NSStringFromClass(viewControl.class)]) {
            return YES;
        }
    }
    
    return NO;
}

- (UIViewController*)findControllerWithClass:(Class)_class
{
    for(UIViewController *viewControl in self.navigationController.viewControllers) {
        if ([NSStringFromClass(_class) isEqualToString:NSStringFromClass(viewControl.class)]) {
            return viewControl;
        }
    }
    return nil;
}

- (BOOL)existsControllerWithClass:(Class)_class
{
    for(UIViewController *viewControl in self.navigationController.viewControllers) {
        if ([NSStringFromClass(_class) isEqualToString:NSStringFromClass(viewControl.class)]) {
            return YES;
        }
    }
    return NO;
}

#pragma 回车关闭键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    CLog(@"text:%@",text);
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)addControl:(id)contr forKey:(NSString *)key
{
    [self.controls setObject:contr forKey:key];
}

- (void)addControl:(id)contr value:(id)value forKey:(NSString *)key
{
    [self.controls setObject:contr forKey:key];
    if (value) [self.data setObject:value forKey:key];
}

- (void)autoPullRefresh
{
    [_contentTable setContentOffset:CGPointMake(0, -75) animated:YES];
    [self performSelector:@selector(doPullRefresh) withObject:nil];
}

//执行下拉刷新
-(void)doPullRefresh {
    [_refreshHeaderView egoRefreshScrollViewDidScroll:_contentTable];
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:_contentTable];
}

-(void)createHeaderViewForTable:(UITableView *)tableview {
    self.contentTable = tableview;
    
    if (_refreshHeaderView && [_refreshHeaderView superview]) {
        [_refreshHeaderView removeFromSuperview];
    }
	_refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:
                          CGRectMake(Zero, Zero - ScreenHeight, ScreenWidth, ScreenHeight)];
    _refreshHeaderView.delegate = self;
    
	[_contentTable addSubview:_refreshHeaderView];
    
    [_refreshHeaderView refreshLastUpdatedDate];
}

#pragma mark data reloading methods that must be overide by the subclass
-(void)beginToReloadData:(EGORefreshPos)aRefreshPos
{
	_reloading = YES;
    
    if (aRefreshPos == EGORefreshHeader)
	{
        [self performSelector:@selector(refreshView) withObject:nil afterDelay:2.0];
    }
}

//刷新调用的方法
-(void)refreshView
{
//    _dataSource.pageNumber = 1;
//    [_dataSource fetchActivity];
}

-(void)finishedLoadData
{
    [_contentTable reloadData];
    [self finishReloadingData];
}

- (void)finishReloadingData
{
	_reloading = NO;
    
	if (_refreshHeaderView) {
        [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:_contentTable];
    }
}

#pragma mark -
#pragma mark EGORefreshTableDelegate Methods
- (void)egoRefreshTableDidTriggerRefresh:(EGORefreshPos)aRefreshPos
{
	[self beginToReloadData:aRefreshPos];
}

- (BOOL)egoRefreshTableDataSourceIsLoading:(UIView*)view
{
	return _reloading; // should return if data source model is reloading
}

// if we don't realize this method, it won't display the refresh timestamp
- (NSDate*)egoRefreshTableDataSourceLastUpdated:(UIView*)view
{
	return [NSDate date]; // should return date data source was last changed
}

@end
